package com.cpe.web.intercepter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cpe.web.intercepter.factory.SepararteInterceptorFactory;

import lombok.extern.slf4j.Slf4j;

/**
 * 分流拦截器，根据请求源分流
 * 
 * @description TODO
 * @author zhoud
 * @date 2015年11月10日 下午1:49:02
 */
@Slf4j
public class SeparateInterceptor extends HandlerInterceptorAdapter {

	private SepararteInterceptorFactory factory;

	public SeparateInterceptor() {
		factory = new SepararteInterceptorFactory();
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String source = request.getHeader("Source");
		log.info("source=" + source);
		List<HandlerInterceptor> interceptors = factory.getInterceptors(source);
		for (HandlerInterceptor interceptor : interceptors) {
			// 只要有一个没通过，就中断请求
			if (!interceptor.preHandle(request, response, handler)) {
				return false;
			}
		}
		return true;
	}

}
